package com.jmu.aslongastoday.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jmu.aslongastoday.common.Result;
import com.jmu.aslongastoday.domain.LogBrowsing;
import com.jmu.aslongastoday.domain.TestQuestion;
import com.jmu.aslongastoday.domain.TestScore;
import com.jmu.aslongastoday.dto.JudgeTestInputDto;
import com.jmu.aslongastoday.dto.JudgeTestOutputDto;
import com.jmu.aslongastoday.dto.TestQuestionDto;

import com.jmu.aslongastoday.service.TestQuestionService;
import com.jmu.aslongastoday.service.TestScoreService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("test")
public class TestController {
    @Resource
    Gson gson;

    @Resource
    TestQuestionService testQuestionService;

    @Resource
    TestScoreService testScoreService;

    @GetMapping("start_test")
    public Result getTest(Integer testType){
        //查询数据库
        List<TestQuestion> testQuestionList = testQuestionService.list();


        //根据类型取题库
        ArrayList<TestQuestionDto> list = new ArrayList<>();

        HashSet<Integer> set = new HashSet<>();
        int start = (testType - 1) * 10;

        for(int i = start; i < start +10;i++){
            //testType = 0 抽选题目
            if(testType == 0){
                int index;
                while (true){
                    index = RandomUtils.nextInt(0,testQuestionList.size()-1);
                    if(!set.contains(index)){
                        set.add(index);
                        break;
                    }
                }
            }else {
                set.add(i);
            }
        }

        for (Integer index : set) {
            TestQuestion question = testQuestionList.get(index);


            TestQuestionDto testQuestion = new TestQuestionDto();
            testQuestion.setId(question.getId());
            testQuestion.setTitle(question.getTitle());
            testQuestion.setCheck(-1);

            String answersJson = question.getAnswers();

            Type type = new TypeToken<List<String>>() {}.getType();
            List<String> answers = gson.fromJson(answersJson, type);
//            ArrayList<String> answers = new ArrayList<>();


            testQuestion.setAnswers(answers);
            list.add(testQuestion);
        }


        //TODO 将这百条数据的对应id以及result存入redis



        return Result.success(list);
    }

    @PostMapping("judge_test")
    public Result judgeTest(@RequestBody List<JudgeTestInputDto> dto,@RequestParam String userName){
        JudgeTestOutputDto outputDto = new JudgeTestOutputDto();
        Integer correctNum = 0;
        Integer failNum = 0;
        Integer score = 0;
        ArrayList<TestQuestion> list = new ArrayList<>();
        List<Boolean> caseList= new ArrayList<>();

        for (JudgeTestInputDto e : dto) {
            Integer id = e.getId();
            TestQuestion question = testQuestionService.getById(id);
            list.add(question);

            if(question.getResult().equals(e.getCheck())){
                //答对
                correctNum ++;
                caseList.add(true);
            }else {
                failNum ++;
                caseList.add(false);
            }
        }
        //保存
        outputDto.setCaseList(caseList);
        outputDto.setCorrectNum(correctNum);
        outputDto.setFailNum(failNum);
        outputDto.setScore(correctNum*10);
        outputDto.setTestQuestionsList(list);


        //得分存到数据库
        TestScore testScore = new TestScore();
        testScore.setScore(correctNum*10);
        testScore.setCommitTime(new Date());
        testScore.setExamName("红色精神在线测评");
        testScore.setUserName(userName);
        testScoreService.save(testScore);

        return Result.success(outputDto);
    }

    @GetMapping("get_score")
    public Result getScore(@RequestParam String userName){
        LambdaQueryWrapper<TestScore> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TestScore::getUserName,userName);
        queryWrapper.orderByDesc(TestScore::getId);

        List<TestScore> list = testScoreService.list(queryWrapper);
        if(list.size()>=5){
            return Result.success(list.subList(0,5));
        }else {
            return Result.success(list);
        }
    }
}
